package com.cdtu.ui.dialog;

import com.cdtu.backend.Application;
import com.cdtu.ui.io.MyInputStream;
import com.cdtu.ui.io.MyPrintStream;

import javax.swing.*;
import java.awt.*;

/**
 * @author Mika
 * @date 2023/08/20 18:09
 * BootDialog 类：游戏启动对话框
 **/
public class BootDialog extends JDialog {
    JTextArea out = new JTextArea(10, 100);
    JTextField in = new JTextField(3);
    JScrollPane scrollPane = null;
    // 宽和高
    public static int WIDTH = 1280;
    public static int HEIGHT = 730;
    private BootDialog() {
        this.setLocationByPlatform(true);
        this.setVisible(true);
        this.setSize(new Dimension(WIDTH, HEIGHT));
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        resetOut();
        resetIn();
        // Pane
        scrollPane = new JScrollPane(out);
        add(scrollPane, BorderLayout.CENTER);
        add(in, BorderLayout.SOUTH);
        new Thread(Application::run).start();
    }
    // 获取到单例对象
    public static BootDialog getInstance() {
        return Holder.INSTANCE;
    }
    // 获取非单例对象
    public static BootDialog getNormal() {
        return new BootDialog();
    }
    // Holder
    private static class Holder {
        private final static BootDialog INSTANCE = new BootDialog();
    }
    // 重定向输出
    private void resetOut() {
        out.setFont(new Font("宋体", Font.PLAIN, 16));
        out.setEditable(false);
        MyPrintStream mps = new MyPrintStream(System.out, out);
        System.setOut(mps);
        System.setErr(mps);
    }
    // 重定向输入
    private void resetIn() {
        MyInputStream mis = new MyInputStream(in);
        in.addActionListener(mis);
        System.setIn(mis);
    }
}
